unit ExampleModule;

interface

uses
  System.SysUtils, System.Classes, System.JSON,
  Sparkle.HttpServer.Context,
  EnhancedServerModule, UnifiedResponse;

type
  TExampleModule = class(TEnhancedServerModule)
  public
    constructor Create(const ABaseUri: string); override;

  
    [TRoute('GET', '/hello')]
    procedure GetHello(Context: THttpServerContext);

    [TRoute('POST', '/hello')]
    procedure PostHello(Context: THttpServerContext);

    [TRoute('PUT', '/hello')]
    procedure PutHello(Context: THttpServerContext);

    [TRoute('DELETE', '/hello')]
    procedure DeleteHello(Context: THttpServerContext);

 
    [TRoute('GET', '/greet/{name}')]
    procedure GreetUser(Context: THttpServerContext);

    [TRoute('GET', '/items/{category}/{id}')]
    procedure GetItem(Context: THttpServerContext);
 
    [TRoute('GET', '/version')]
    [TRoute('GET', '/info')]
    [TRoute('GET', '/about')]
    procedure GetVersion(Context: THttpServerContext);
  end;

implementation

{ TExampleModule }

constructor TExampleModule.Create(const ABaseUri: string);
begin
  inherited Create(ABaseUri);
  LogInfo('Example module initialized with advanced routing');
end;

procedure TExampleModule.GetHello(Context: THttpServerContext);
begin
  TApiResponseHelper.SendSuccess(Context,
    '{"message": "Hello World (GET)", "timestamp": "' +
    FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now) + '"}',
    'GET request handled');
end;

procedure TExampleModule.PostHello(Context: THttpServerContext);
begin
  TApiResponseHelper.SendSuccess(Context,
    '{"message": "Hello World (POST)", "timestamp": "' +
    FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now) + '"}',
    'POST request handled');
end;

procedure TExampleModule.PutHello(Context: THttpServerContext);
begin
  TApiResponseHelper.SendSuccess(Context,
    '{"message": "Hello World (PUT)", "timestamp": "' +
    FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now) + '"}',
    'PUT request handled');
end;

procedure TExampleModule.DeleteHello(Context: THttpServerContext);
begin
  TApiResponseHelper.SendSuccess(Context,
    '{"message": "Hello World (DELETE)", "timestamp": "' +
    FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now) + '"}',
    'DELETE request handled');
end;

procedure TExampleModule.GreetUser(Context: THttpServerContext);
var
  UserName: string;
begin
 
  UserName := 'User';  

  TApiResponseHelper.SendSuccess(Context,
    Format('{"greeting": "Hello, %s!", "timestamp": "%s"}',
      [UserName, FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now)]),
    'Personalized greeting');
end;

procedure TExampleModule.GetItem(Context: THttpServerContext);
begin
  TApiResponseHelper.SendSuccess(Context,
    '{"item": "Sample item", "category": "sample", "id": "123", "timestamp": "' +
    FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now) + '"}',
    'Item details');
end;

procedure TExampleModule.GetVersion(Context: THttpServerContext);
begin
  TApiResponseHelper.SendSuccess(Context,
    '{"module": "example", "version": "1.0.0", "timestamp": "' +
    FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now) + '"}',
    'Module information');
end;

end.